package gamePlay;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Control class for a single player game.
 * 
 * @author Anthony Dare
 * 
 */
public class SinglePlayerControl {
	static Player me;
	
	public static void main(String args[]) {
		final int rounds = 7;
		int numPlayers;
		Game singleGame;
		String playerName;
		List<ComputerPlayer> compPlayers = new ArrayList<ComputerPlayer>();
		int playerAnswer;
		List<Bet> playerBets;

		numPlayers = getNumPlayers();
		singleGame = new Game("Single Player", numPlayers);
		playerName = getPlayerName();

		me = new Player(playerName);
		me.joinGame(singleGame);

		// Generate computer players.
		for (int i = 0; i < numPlayers - 1; i++) {
			ComputerPlayer comp = new ComputerPlayer("Computer_" + i);
			comp.joinGame(singleGame);
			compPlayers.add(comp);
		}

		// Begin game.
		System.out.println("*******************************");
		System.out.println("Let the game begin!");
		System.out.println();
		for (int round = 0; round < rounds; round++) {
			// Display Question
			singleGame.displayQuestion();

			// Get answer from player
			playerAnswer = getPlayerAnswer();
			me.submitGuess(playerAnswer);

			// Submit computer guesses
			int answer = singleGame.getQuestion().getAnswer();
			for (int i = 0; i < numPlayers - 1; i++) {
				compPlayers.get(i).submitGuess(answer);
			}

			// Get bets from player
			singleGame.displayGuesses();
			System.out.println();
			playerBets = getPlayerBets(singleGame.getGuesses());
            for(int i = 0; i < playerBets.size(); i++){
/************** Added true to the end of this statement to bet permanent chpis, revise later ***************************/
            	me.placeBet(playerBets.get(i).getChoice(), playerBets.get(i).getPermaChips(), playerBets.get(i).getGameChips()); 
            }
            
            // Computers bet
            for(int i = 0; i < numPlayers - 1; i ++){
            	compPlayers.get(i).chooseBet();
            }
            
            
			
            // Show Winnings
            singleGame.getBets();
            System.out.println();
            singleGame.winnings();
            System.out.println(me.getPlayer_name() + " now has " + me.getGame_chips() + " game chips.");
            for(int i = 0; i < compPlayers.size(); i++){
            	System.out.println(compPlayers.get(i).getPlayer_name() + " now has " + compPlayers.get(i).getGame_chips() + " game chips.");
            }
            System.out.println();
            
            
            // Show Answer
            singleGame.displayAnswer();
            System.out.println();
		}
		System.out.println("GAME OVER");
	}

	/**
	 * Get the players name. To be modified for use with GUI.
	 * 
	 * @return
	 */
	private static String getPlayerName() {
		String playerName;
		System.out.print("Please input your name:");
		Scanner keyboard = new Scanner(System.in);
		playerName = keyboard.nextLine();
		System.out.println();
		return playerName;
	}

	/**
	 * Get the number of players. To be modified for use with GUI.
	 * 
	 * @return
	 */
	private static int getNumPlayers() {
		int numPlayers = 0;
		while (numPlayers < 3 || numPlayers > 6) {
			System.out.print("Please input the number of players(3-6):");
			Scanner keyboard = new Scanner(System.in);
			numPlayers = keyboard.nextInt();
		}
		System.out.println();
		return numPlayers;

	}

	/**
	 * Get the answer from the player. To be modified for use with GUI.
	 * 
	 * @return
	 */
	private static int getPlayerAnswer() {
		System.out.print("Please enter the answer now: ");
		Scanner keyboard = new Scanner(System.in);
		int ans = keyboard.nextInt();
		System.out.println();
		return ans;
	}
	
	/**
	 * Get the player's bets. To be modified for use with GUI.
	 * TODO: Checking on player chip amounts.
	 * 
	 * @return
	 */
	private static List<Bet> getPlayerBets(List<Guess> guesses) {
		Scanner keyboard = new Scanner(System.in);
		List<Bet> bets = new ArrayList<Bet>();
		int choice = -1;
		int permaChips = 0;
		int tempChips = 0;
		System.out.println("Place your bets now.");
		do {
			System.out
					.print("On which guess do you wish to place a bet(-1 to end betting): ");
			choice = keyboard.nextInt();
			if (choice >= 0 && choice < guesses.size()) {
				System.out
						.print("How many permanent chips would you like to bet: ");
				permaChips = keyboard.nextInt();
				System.out
						.print("How many temporary chips would you like to bet: ");
				tempChips = keyboard.nextInt();
				bets.add(new Bet(me.getPlayer_name(), choice, permaChips, tempChips));
			}
		} while (choice != -1);
		System.out.println();
		return bets;
	}
}